//
//  GoodsModel+net.m
//  星期6
//
//  Created by 宗镇荣 on 16/2/24.
//  Copyright © 2016年 zzr. All rights reserved.
//

#import "GoodsModel+net.h"
#import "NetDefine.h"
#import "AFNetworking.h"

@implementation GoodsModel (net)

+ (void)requestPage:(NSInteger)page completionBlock:(CompletionBlock)block {

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:kGoodsUrl parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSDictionary *rootDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDict = rootDict[@"data"];
        
        NSArray *itemsArray = dataDict[@"items"];
        NSMutableArray *mArray = [[NSMutableArray alloc] init];
        for (NSDictionary *subDict in itemsArray) {
            
            GoodsModel *model = [[GoodsModel alloc] initWithDictionary:subDict[@"data"] error:nil];
            
//            for (NSDictionary *tempDict in tempdict) {
//                
//                GoodsModel *model = [[GoodsModel alloc] initWithDictionary:tempDict error:nil];
//                NSLog(@"%@", model);
            
               
//            }
        [mArray addObject:model];
        }
        
        block(mArray, nil);
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        block(nil,error);
    }];

}

@end
